﻿using Microsoft.AspNetCore.Components;
using Project13776616780_01.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;

namespace Project13776616780_01.Pages
{
    public partial class Template
    {
        [Inject]
        private DocsDbContext dbContext { get; set; }
        private string code;

        protected override void OnInitialized()
        {
            base.OnInitialized();
            var template = dbContext.Set<AppSetting>().FirstOrDefault(x => x.Name == Enums.AppSettingName.Template);
            code = template?.Value;
        }

        private void SaveData()
        {
            var template = dbContext.Set<AppSetting>().FirstOrDefault(x => x.Name == Enums.AppSettingName.Template);
            if (template == null)
            {
                template = new AppSetting()
                {
                    Name = Enums.AppSettingName.Template,
                    Value = code
                };
                template = dbContext.Set<AppSetting>().Add(template).Entity;
            }
            else
            {
                template.Value = code;
            }
            dbContext.SaveChanges();
            Toast("保存成功，但需要重新生成才会生效");
        }
    }
}
